package com.fanshuai2.niuke.strings;

import java.util.Scanner;

/**
 * 描述
 * 密码要求:
 *
 * 1.长度超过8位
 *
 * 2.包括大小写字母.数字.其它符号,以上四种至少三种
 *
 * 3.不能有长度大于2的包含公共元素的子串重复 （注：其他符号不含空格或换行）
 *
 * 数据范围：输入的字符串长度满足
 * 1
 * ≤
 * �
 * ≤
 * 100
 *
 * 1≤n≤100
 * 输入描述：
 * 一组字符串。
 *
 * 输出描述：
 * 如果符合要求输出：OK，否则输出NG
 *
 * 输入：
 * 021Abc9000
 * 021Abc9Abc1
 * 021ABC9000
 * 021$bc9000
 * 输出：
 * OK
 * NG
 * NG
 * OK
 */

public class Password {
    static boolean isDigit(char ch) {
        return ch >= '0' && ch <= '9';
    }
    static boolean isLower(char ch) {
        return ch >= 'a' && ch <= 'z';
    }
    static boolean isUpper(char ch) {
        return ch >= 'A' && ch <= 'Z';
    }

    static boolean isValid(String s) {
        if (null == s || s.length() <= 8) {
            return false;
        }

        char[] chs = s.toCharArray();
        int digit = 0;
        int lower = 0;
        int upper = 0;
        int others = 0;
        for (char ch: chs) {
            if (isDigit(ch)) {
                digit = 1;
            } else if (isLower(ch)) {
                lower = 1;
            } else if (isUpper(ch)) {
                upper = 1;
            } else {
                others = 1;
            }
        }
        int num = digit + lower + upper + others;
        if (num < 3) {
            return false;
        }

        int len = chs.length;
        boolean repeat = false;
        int repeatNum = 0;
        for (int i = 0; i < len - 2; i++) {
            int pos = i;
            int j = pos + 1;

            while (j < len) {
                repeatNum = 0;
                while (i < len && j < len && chs[i] == chs[j]) {
                    boolean b = chs[i] == chs[j];
                    repeatNum++;
                    i++;
                    j++;
                }

                if (repeatNum > 2) {
                    repeat = true;
                    break;
                }
                i = pos;
                while (j < len && chs[i] != chs[j]) {
                    j++;
                }
            }

            if (repeat) {
                break;
            }
            i = pos;
        }
        return !repeat;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        in.next();

        while (in.hasNextLine()) {
            String s = in.nextLine();
            boolean valid = isValid(s);
            if (valid) {
                System.out.println("OK");
            } else {
                System.out.println("NG");
            }
        }
    }
}
